﻿using System;
using System.Windows.Forms;
using org.se.fs.presenter.View.Profile;

namespace org.se.fs.view.Profile
{
    public partial class FrmClientLists : Form, presenter.IView.Template.IFormLists
    {
        #region IFormLists Members
        public string StatusPage
        {
            get { return lblStatusPage.Text; }
            set { lblStatusPage.Text = value; }
        }
        public int IndexType
        {
            get { return cmbType.SelectedIndex; }
            set { cmbType.SelectedIndex = value; }
        }
        public string SearchItem
        {
            get { return txtItem.Text; }
            set { txtItem.Text = value; }
        }
        public void FillType(object[] data)
        {
            cmbType.Items.Clear();
            cmbType.Items.AddRange(data);
        }
        public void EnableButtonSearch(bool enable)
        {
            btnSearch.Enabled = enable;
        }
        public void EnableResults(bool enable)
        {
            groupBox.Enabled = enable;
        }
        public object Lists
        {
            get { return dataGridView.DataSource; }
            set { dataGridView.DataSource = value; }
        }
        public string StatusLists
        {
            get { return groupBox.Text; }
            set { groupBox.Text = value; }
        }
        public void EnableButtonAdd(bool enable)
        {
            btnAdd.Enabled = enable;
        }
        public void EnableButtonRecordFirst(bool enable)
        {
            btnRecordFirst.Enabled = enable;
        }
        public void EnableButtonRecordPrevious(bool enable)
        {
            btnRecordPrevious.Enabled = enable;
        }
        public void EnableButtonRecordNext(bool enable)
        {
            btnRecordNext.Enabled = enable;
        }
        public void EnableButtonRecordLast(bool enable)
        {
            btnRecordLast.Enabled = enable;
        }
        #endregion

        private readonly ClientLists _presenter;
        private static FrmClientLists _frmForm;

        public FrmClientLists()
        {
            InitializeComponent();
            _presenter = new ClientLists(this);
            _presenter.OnError += PresenterOnError;
            _presenter.OnDelete += PresenterOnDelete;
            dataGridView.AutoGenerateColumns = false;
        }

        void PresenterOnError(object source, presenter.Library.FormEvent e)
        {
            if (e.IsSuccess)
                MessageBox.Show(e.Exception.Message, @"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        static void PresenterOnDelete(object source, presenter.Library.FormEvent e)
        {
            MessageBox.Show(e.Exception.Message, e.IsSuccess ? "Success" : "Failed", MessageBoxButtons.OK,
                            e.IsSuccess ? MessageBoxIcon.Information : MessageBoxIcon.Error);
        }

        public static void FormShow(Form form)
        {
            if (_frmForm == null)
            {
                _frmForm = new FrmClientLists {MdiParent = form};
                _frmForm.Show();
            }
            else
                _frmForm.Focus();
        }

        private void BtnCloseClick(object sender, EventArgs e)
        {
            Close();
        }

        private void BtnAddClick(object sender, EventArgs e)
        {
            var data = FrmClientSubMenu.FormShow(_presenter.EmptyRecord());
            _presenter.Add(data);
        }

        private void FrmFamilyBackgroundLoad(object sender, EventArgs e)
        {
            _presenter.Initialize();
        }

        private void DataGridViewCellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (!(e.RowIndex >= 0 && e.ColumnIndex >= 0 && e.RowIndex < dataGridView.Rows.Count && e.ColumnIndex < 2))
                return;

            var data = _presenter.GetData(e.RowIndex);

            if (e.ColumnIndex.Equals(0))
                _presenter.Modify(FrmClientRecords.FormShow(data));

            else
            {
                var dialogResult = MessageBox.Show(@"Are you sure you want to delete?", @"Confirm", MessageBoxButtons.YesNo,
                                                   MessageBoxIcon.Question);
                if (dialogResult.Equals(DialogResult.No))
                    return;
                _presenter.Remove(data);
            }
        }

        private void FrmClientListsFormClosed(object sender, FormClosedEventArgs e)
        {
            _frmForm = null;
        }
    }
}